package com.uusee.crawler.job;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.uusee.crawler.dbwriter.UgcDbWriter;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.framework.AbstractJob;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.video.sohu.SohuVideoInfoInterfacePageProcessor;
import com.uusee.crawler.xmlwriter.UgcXmlWriter;
import com.uusee.shipshape.sp.model.Ugc;

public class SohuUgcFastCrawlJob extends AbstractJob {
	private FetchHTTP fetch = new FetchHTTP();
	private SohuVideoInfoInterfacePageProcessor pageProcessor = new SohuVideoInfoInterfacePageProcessor();
	private UgcXmlWriter xmlwriter = new UgcXmlWriter();
	private UgcDbWriter dbWriter = new UgcDbWriter();

	public static void main(String[] args) {
		try {
			ApplicationContext acx = new ClassPathXmlApplicationContext("applicationContext-ugc.xml");
			SohuUgcFastCrawlJob sohuUgcFastCrawlJob = (SohuUgcFastCrawlJob) acx.getBean("sohuUgcFastCrawlJob");
			sohuUgcFastCrawlJob.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public CrawlURI createCrawlURI(String vid) {
		String playUrl = "http://v.blog.sohu.com/u/vw/" + vid;
		Ugc ugc = new Ugc();
		ugc.setVid(vid);
		ugc.setVideoSite("v.blog.sohu.com");
		ugc.setPlayUrl(playUrl);
		ugc.setStatus("0");

		CrawlURI crawlURI = new CrawlURI();
		crawlURI.setSourceSite("v.blog.sohu.com");
		crawlURI.setCrawlUrl("http://v.blog.sohu.com/videinfo.jhtml?outType=3&m=get&id=" + vid);
		crawlURI.setModel(ugc);
		crawlURI.setCharset("utf-8");
		
		return crawlURI;
	}

	public void crawl(CrawlURI crawlURI) {
		try {
			fetch.process(crawlURI);
			pageProcessor.process(crawlURI);
			// xmlwriter.process(crawlURI);
			dbWriter.process(crawlURI);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setFetch(FetchHTTP fetch) {
		this.fetch = fetch;
	}

	public void setPageProcessor(SohuVideoInfoInterfacePageProcessor pageProcessor) {
		this.pageProcessor = pageProcessor;
	}

	public void setXmlwriter(UgcXmlWriter xmlwriter) {
		this.xmlwriter = xmlwriter;
	}

	public void setDbWriter(UgcDbWriter dbWriter) {
		this.dbWriter = dbWriter;
	}
}
